﻿<?php
class Category_Model_Categories extends Zend_Db_Table{
	protected $_name = 'categories';
	protected $_primary = 'category_id';
	/* Link tham khao : http://framework.zend.com/manual/en/zend.db.table.html */
	/* select demo */
	public function getAllCategories()
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/categories/route/getAllCategories");
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);

		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		return $data;
	}
	public function getCategoriesbyId($category_id)
	{//co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/categories/route/getCategoriesbyId/category_id/".$category_id);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);

		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		return $data;
	}
	
	public function getParentProductCategories()
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/categories/route/getParentProductCategories");
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
		
		return $data;
	}
	public function getLeafProductCategories()
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/categories/route/getLeafProductCategories");
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
		
		return $data;
	}
	public function getParentCategoriesbyType($type)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/categories/route/getParentCategoriesbyType/type/".$type);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);

		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		return $data;
	}
	
	public function getLeafCategoriesbyType($type)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/categories/route/getLeafCategoriesbyType/type/".$type);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);

		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		return $data;
	}
	
	public function getLeafCategoriesByParent($parentid)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/categories/route/getLeafCategoriesByParent/parentid/".$parentid);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		return $data;
	}
	
	public function addCategory($data)
	{ // co dung
		$data['route'] = "addCategory";
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restPost("/categories", $data);

		return $return->getBody();
	}
	
	public function updateIsLeaf($id,$is_leaf)
	{//co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restPut("/categories/route/updateIsLeaf/id/".urlencode($id)."/is_leaf/".urlencode($is_leaf));
		return $return->getBody();
	}
	
	public function updateName($id,$name)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restPut("/categories/route/updateName/id/".urlencode($id)."/name/".urlencode($name));
		return $return->getBody();
	}
	
	public function updateParentId($id,$parent_id)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restPut("/categories/route/updateParentId/id/".urlencode($id)."/parent_id/".urlencode($parent_id));
		return $return->getBody();
	}
	
	public function deleteCategoryById($id)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restDelete("/categories/route/deleteCategoryById/id/".$id);
		return $return->getBody();
	}
	
}
?>